package com.ww.scala.syntax

import java.util

/**
 * 集合
 */
object syntax03_collections {

  def main(args: Array[String]) : Unit = {
    //scala 中的泛型用[]
    val strings = new util.LinkedList[String]()
    strings.add("")

    //数组
    val ints = Array[Int](1, 2, 3, 4)
    println(ints(1))

    println("===================list======================")
    val ints1 = List(1, 2, 3, 4, 5)
    for (elem <- ints1) {

    }
    ints1.foreach(println)
    println("===================set======================")
    val ints2 = Set[Int](1, 2, 3, 4, 3, 4, 3, 4)
    ints2.foreach(println)

    //不可变
    import scala.collection.mutable.Set
    val ints3 = Set(1, 2, 3, 4, 3, 2)
    ints3.add(5)
    ints3.foreach(println)
    println("===================tuple======================")
    val t2 = new Tuple2(1, "xxx")
    val t3 = Tuple3(1, "22", '3')
    val t4 = (1, 2, 3, 4)
    println(t2._2)
    println(t4._3)
    println("===================map======================")
    val map01 = Map[String, Int](("a", 1), "b" -> 2, ("a", 3))
    val keys = map01.keys
    println(map01.get("a").get)
    println(map01.get("b"))

    println(map01.get("w").getOrElse("hello world"))

    println("===================流式api======================")

    val ints4 = List(1, 2, 3, 4, 5, 6, 7)
    ints4.foreach(println)
    val ints5 = ints4.map(_ + 10)
    ints5.foreach(println)
    println("===================流式api flatMap======================")
    val list = List[String]("hello 1", "hello 2", "hello 3")
    val list1 = list.flatMap(_.split(" "))
    list1.foreach(println)
    val tuples = list1.map((_, "value"))
    tuples.foreach(println)
    println("===================流式api 迭代器======================")
    val iterator = list.iterator
    val strings1 = iterator.flatMap((x)=>{
      println("执行了")
      x.split(" ")
    })
    strings1.foreach(println)

    //val tuples1 = iterator.map((_, "value"))
    //tuples1.foreach(println)
  }


}
